/*
* Copyright (c) 2016 . All Rights Reserved.
*/
package com.cgmnx.admin.web.converter.vm;

import com.cgmnx.admin.biz.rbac.RoleBiz;
import com.cgmnx.admin.common.util.DateUtil;
import com.cgmnx.admin.dao.entity.rbac.RoleEntity;
import com.cgmnx.admin.dao.entity.rbac.UserEntity;
import com.cgmnx.admin.model.enums.rbac.UserStatus;
import com.cgmnx.admin.web.converter.Convert;
import com.cgmnx.admin.web.model.vm.UserVm;
import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableListMultimap;
import com.google.common.collect.Multimaps;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.List;

/**
 * @author: created  Date: 16/12/18 Time: 下午5:29
 * @version: 1.0
 */
@Component
public class UserVmConverter implements Convert<UserEntity, UserVm> {

    private static final Joiner JOINER = Joiner.on(",").skipNulls();
    @Autowired
    private RoleBiz roleBiz;

    @Override
    public UserVm convert(UserEntity t) {

        if (t == null) {
            return null;
        }

        UserVm vm = new UserVm();

        vm.setUserId(t.getUserId());
        vm.setUserName(t.getUserName());
        vm.setUpdateUser(t.getUpdateUser());
        vm.setCreateUser(t.getCreateUser());
        vm.setCreateTime(DateUtil.formatDatetime(t.getCreateTime()));
        vm.setUpdateTime(DateUtil.formatDatetime(t.getUpdateTime()));
        vm.setId(t.getId());
        UserStatus status = t.getStatus();
        if (status != null) {
            vm.setStatusText(status.getDesc());
            vm.setStatusCode(status.getCode());
        } else {
            vm.setStatusText(StringUtils.EMPTY);
            vm.setStatusCode(NumberUtils.INTEGER_ZERO);
        }
        vm.setUserEmail(t.getUserEmail());
        vm.setUserPhone(t.getUserPhone());
        vm.setPassword(t.getPassword());
        vm.setDesc(t.getDescription());
        return vm;
    }

    @Override
    public List<UserVm> convert(List<UserEntity> t) {

        if (CollectionUtils.isEmpty(t)) {
            return Collections.emptyList();
        }


        final List<String> userIdList = FluentIterable.from(t).transform(new Function<UserEntity, String>() {
            @Override
            public String apply(UserEntity input) {
                if (input == null) {
                    return null;
                }
                return input.getUserId();
            }
        }).toList();

        final ImmutableListMultimap<String, RoleEntity> index = getRoleListMultimap(userIdList);

        List<UserVm> userVmImmutableList = FluentIterable.from(t).transform(new Function<UserEntity, UserVm>() {

            @Override
            public UserVm apply(UserEntity input) {

                if (input == null) {
                    return null;
                }

                ImmutableList<RoleEntity> roleEntities = index.get(input.getUserId());
                UserVm userVm = convert(input);
                if (CollectionUtils.isNotEmpty(roleEntities)) {
                    userVm.setRoleName(JOINER.join(FluentIterable.from(roleEntities).transform(new Function<RoleEntity, String>() {

                        @Override
                        public String apply(RoleEntity input) {
                            return input.getName();
                        }
                    })));
                    ImmutableList<Integer> integers = FluentIterable.from(roleEntities).transform(new Function<RoleEntity, Integer>() {

                        @Override
                        public Integer apply(RoleEntity input) {
                            return input.getId();
                        }
                    }).toList();
                    userVm.setRoleId(integers);
                }
                return userVm;
            }
        }).toList();

        return userVmImmutableList;
    }

    private ImmutableListMultimap<String, RoleEntity> getRoleListMultimap(List<String> userIdList) {

        if (CollectionUtils.isEmpty(userIdList)) {
            return ImmutableListMultimap.of();
        }

        List<RoleEntity> roleEntityList = roleBiz.queryByUserIdList(userIdList);
        if (CollectionUtils.isEmpty(roleEntityList)) {
            return ImmutableListMultimap.of();
        }


        return Multimaps.index(roleEntityList, new Function<RoleEntity, String>() {

            @Override
            public String apply(RoleEntity input) {
                return input.getUserId();
            }
        });
    }

}